// import { useKline } from "@/stores/kline";
import { Bus, DAY_STATE, KLINE_RANK_FLUCTUATION, KLINE_RANK_FLUCTUATION_MN5, KLINE_RANK_FLUCTUATION_ALL } from "./Bus";
import { IBroadcast } from "./IBroadcast";

/**
 * 顶部币种的大盘数据
 */
export class WaveBroadcast extends IBroadcast {
    // channelParam: any;
    channelStr = "";
    //榜单
    channelParam = {
        // group: "MN5",
        // event: "subscribe",
        // topic: "FLUCTUATION"
        interval: "MN5",
        group: "ALL",
        event: "subscribe",
        topic: "FLUCTUATION"
    };
    //右侧
    sideParam = {
        interval: "D1",
        group: "TOP20",
        event: "subscribe",
        topic: "FLUCTUATION"
    };
    channelM5Str = "";

    constructor() {
        super();
        // const kline = useKline();
        this.channelM5Str = JSON.stringify(this.channelParam);
        let s = localStorage.getItem("K_TIME");
        if(s){
            this.sideParam.interval = s == "S1" ? "D1" : s;
        }
    }


    changeParam(timer: string) {
        this.channelParam.interval = timer;
        this.subscribe();
    }
    changeTime(timer: string) {
        this.sideParam.interval = timer == "S1" ? "D1" : timer;
        this.subscribe();
    }
    subscribe(): void {
        // console.log('初始化订阅======================#################')
        this.unSubscribe();
        if (!this.channelParam) {
            // console.log("订阅参数未初始化");
            return;
        }
        this.channelStr = JSON.stringify(this.channelParam);
        this.channelM5Str = JSON.stringify(this.sideParam);

        // console.error("day === sub", this.channelStr)
        // this.socket.send(this.channelStr);
        if(location.href.includes("/ranks")){
            this.send(this.channelStr);
            // console.log("开始订阅", this.channelStr)
        }
        
        this.send(this.channelM5Str);
        // console.log("开始订阅", this.channelM5Str)
    }

    unSubscribe(): void {
        if (!this.channelStr) return;
        const obj = JSON.parse(this.channelStr);
        obj.event = "unsubscribe";
        this.send(JSON.stringify(obj));
        // console.log("取消订阅", JSON.stringify(obj))
        const obj1 = JSON.parse(this.channelM5Str);
        obj1.event = "unsubscribe";
        this.send(JSON.stringify(obj1));
        // console.log("取消订阅", JSON.stringify(obj1))
    }

    handle(res: any): void {
        // console.log("##jsz", res, "ddddr");
        // throw new Error("Method not implemented.");
        // console.log(res.group, this.channelParam.group, "websockletllll");
        if (res.data) {
            // Bus.emit(DAY_STATE, res.data);
            // if (this.sideParam.group == res.group) {
            //     Bus.emit(KLINE_RANK_FLUCTUATION_MN5, res.data);
            // }
            if(res.group == "TOP20"){
                Bus.emit(KLINE_RANK_FLUCTUATION_MN5, res.data);
            }
            else{
                // if (this.channelParam.group == res.group) {
                    Bus.emit(KLINE_RANK_FLUCTUATION, res.data);
                // }
            }
        }
    }
}
